﻿using Autofac;
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wittur.GenereateKongCoCPDF.Core.Extensions
{
    public static class AutoMapperExtensions
    {
        public static ContainerBuilder ConfigureAutoMapper(this ContainerBuilder builder)
        {
            // 注册所有 Profile
            builder.RegisterAssemblyTypes(typeof(AutoMapperExtensions).Assembly)
                .AssignableTo<Profile>()
                .As<Profile>();

            // 注册 AutoMapper 配置
            builder.Register(ctx => {
                var profiles = ctx.Resolve<IEnumerable<Profile>>();
                return new MapperConfiguration(cfg => {
                    foreach (var profile in profiles) cfg.AddProfile(profile);
                });
            }).SingleInstance().AsSelf();

            // 注册 IMapper
            builder.Register(ctx => {
                var config = ctx.Resolve<MapperConfiguration>();
                return config.CreateMapper();
            }).As<IMapper>();

            return builder;
        }
    }
}
